package com.woniuxy.ssm.powermag.controller;

import com.woniuxy.ssm.entity.DeptEntity;
import com.woniuxy.ssm.entity.ResEntity;
import com.woniuxy.ssm.powermag.service.IDeptService;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.*;

import javax.annotation.Resource;

@RequestMapping("/dept")
@RestController
public class DeptController {

    @Resource
    private IDeptService deptServiceImpl;

    @PostMapping("/{id}")
    public ResEntity save(DeptEntity deptEntity){
        deptServiceImpl.save(deptEntity);
        return ResEntity.builder().code(200).msg("新增成功").build();
    }

    @PutMapping("/{id}")
    public ResEntity update(DeptEntity deptEntity){
        deptServiceImpl.update(deptEntity);
        return ResEntity.builder().code(200).msg("修改成功").build();
    }

    @DeleteMapping("/{id}")
    public ResEntity delete(@PathVariable("id") Long id){
        deptServiceImpl.deleteById(id);
        return ResEntity.builder().code(200).msg("删除成功").build();
    }

    @GetMapping("/{id}")
    public ResEntity get(@PathVariable("id") Long id){
        DeptEntity dept = deptServiceImpl.getById(id);
        return ResEntity.builder().code(200).msg("查询成功")
                .datas(dept).build();
    }

    @GetMapping("/page")
    public ResEntity page(Integer page,Integer rows,DeptEntity deptEntity){
        Page<DeptEntity> datas = deptServiceImpl.page(page, rows, deptEntity);

        return ResEntity.builder().code(200).msg("查询成功")
                .datas(datas).build();
    }

}
